// component/pay/pay.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isnavigator:{
      type:Boolean,
      value:false
    },
    finsTotalprice:{
      type:String,
      value:0
    },
    token:{
      type:String,
      value:0
    },
    orderno:{
      type:String,
      value:0
    },
    moneyger:{
      type:String,
      value:0
    },
    moneyqiye:{
      type:String,
      value:0
    },
    onddid:{
      type:String,
      value:''
    },
    onamountnum:{
      type:String,
      value:''
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    weixin: [
      {value: '1', name: '微信支付',msg:'1',images:'../../public/icon/icon-wechart.png'},
      {value: '2', name: '个人支付',msg:'2',images:'../../public/icon/icon-wallet.png'},
      {value: '3', name: '企业支付',msg:'3',images:'../../public/icon/icon-enterprise.png'},
    ],
    baytype:0,
    buydaya:[],
    isTrue:false,
    ontrue:false,
    onisenterprise:false,
    LoginFromQYB:wx.getStorageSync('userInfo').LoginFromQYB,

  },

  pageLifetimes: {
    show: function() {
      // 页面被展示
      let that = this
      let  LoginFromQYB = wx.getStorageSync('userInfo').LoginFromQYB
      if(LoginFromQYB==false){
        that.setData({
          weixin: [
            {value: '1', name: '微信支付',msg:'1',images:'../../public/icon/icon-wechart.png'},
            {value: '2', name: '个人支付',msg:'2',images:'../../public/icon/icon-wallet.png'},
            // {value: '3', name: '企业支付',msg:'3',images:'../../public/icon/icon-enterprise.png'},
          ]
        })
      }
    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /******关闭********/
    isTrue(e){
      let that = this;
      that.setData({
        isnavigator:false
      })
    },
    onCheckbox(e){
      let that = this;
      let type = that.data.baytype
      let rate = that.properties
      let finsTotalprice = that.data.finsTotalprice;
      console.log(type);
      console.log(rate);
      if(type==1){
        //订单支付信息保存
        wx.request({
          url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
          data: {
          cmd:'SetOrderPays',
           token:rate.token,
           orderno:rate.orderno,
           AccMoney:0,
           officeId:1,
           AccCorp:0,
           PayPwd:'',
           PayMoney:rate.finsTotalprice,
           VocMoney:rate.onamountnum,
           VocID:rate.onddid,
          },
          method: "get",
          success(res1) {
            console.log(res1)
            if(res1.data.STATUS=='Success'){    
              //调用服务器统一支付接口
              wx.request({
                url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=uniOrder',
                data: {
                //请求后台的分页数据
                //  cmd:'uniOrder',
                token:rate.token,
                orderno:rate.orderno,
                totalFee:rate.finsTotalprice,
                officeId:1
                },
                method: "get",
                success(res) {
                  //调用微信小程序支付功能
                  wx.requestPayment
                  (
                    {
                      "timeStamp": res.data.DATA.timeStamp,
                      "nonceStr": res.data.DATA.nonceStr,
                      "package": "prepay_id="+res.data.DATA.prepay_id,
                      "signType": "MD5",
                      "paySign": res.data.DATA.paySign,
                      "success":function(res){
                        wx.showToast({
                          title: '支付成功',
                          icon: 'success',
                          duration: 1000
                        })
                      },
                      "fail":function(res){
                        console.log(res)
                        wx.showToast({
                          title: '支付失败1',
                          icon: 'error',
                          duration: 1000
                        })
                      },
                      "complete":function(res){//微信支付回调返回成功失败
                        if(res.errMsg=='requestPayment:ok'){
                          wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 1000
                          })
                          setTimeout(function(){
                            let orderno = rate.orderno;
                            that.setData({
                              orderno:orderno,
                              ontrue:true,
                              isnavigator:false
                            })
                        },2000);  

                        }else{
                          wx.showToast({
                            title: '支付失败',
                            icon: 'error',
                            duration: 1000
                          })
                        }
                      }
                    }
                  )
                }
            })
            }else{
              wx.showToast({
                title: '微信支付出现异常',
                icon: 'error',
                duration: 1000
              })
            }
    }
  });
    }
    if(type==2){
      //判断余额是不是大于支付金额
      let that = this
      let moneyger = that.properties.moneyger
      let finsTotalprice = that.properties.finsTotalprice
      if(moneyger*1<finsTotalprice*1){
        wx.showToast({
          title: '余额不足',
          icon: 'error',
          duration: 1000
        })
      }else{
          //个人支付
          var onisTrue = true;
          this.setData({
          isTrue:onisTrue,
          isnavigator:false
          })
      }
    }

    if(type==3){
      //判断余额是不是大于支付金额
      let that = this
      let moneyqiye = that.properties.moneyqiye
      let finsTotalprice = that.properties.finsTotalprice
      if(moneyqiye*1<finsTotalprice*1){
        wx.showToast({
          title: '余额不足',
          icon: 'error',
          duration: 1000
        })
      }else{
     //企业支付
        var onisonisenterpriseTrue = that.data.onisenterprise;
        this.setData({
        isTrue:false,
        isnavigator:false,
        onisenterprise:true
      })
      }
    }

    },
    selectItem(e) {
      let index = e.currentTarget.dataset.index;
      let value = e.currentTarget.dataset.value;
      if(index=="1"){
          var onisTrue = true;
       }
      //console.log(index);
      let weixin = this.data.weixin;
      for(let i = 0; i<weixin.length; i++){
        weixin[i].select = false;
      }
      weixin[index].select = !weixin[index].select;
      this.setData({
        weixin:weixin,
        baytype:value,
      })
    },
    /**充值*/
    topupwallet(e){
      let that = this;
      wx.navigateTo({
        url: '/pages/wallet/wallet',
      })
    },
  }
})
